package com.care.dao.user.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import com.care.dao.user.UserDao;
import com.care.entity.Consult;
import com.care.entity.Doctor;
import com.care.entity.User;

@Repository
public class UserDaoImpl implements UserDao{
	
	@Resource
	private SessionFactory sessionFactory;
	
	@Override
	public void register(String name, String email, String password, String sex) {
		User user = new User(name, password, sex, email);
		this.sessionFactory.getCurrentSession().save(user);
	}

	@Override
	public List<Consult> selAllConsult(User user,int pageNum, int pageSize) {
		try {
			Query query = this.sessionFactory.getCurrentSession().createQuery("from Consult where user=?");
			query.setParameter(0, user);
			query.setFirstResult((pageNum-1)*pageSize);
			query.setMaxResults(pageSize);
			List<Consult> list = query.list();
			return list;
		} catch (Exception e) {
			// TODO: handle exception
		}return null;
	}

	@Override
	public boolean addConsult(Consult consult) {
		Serializable save = this.sessionFactory.getCurrentSession().save(consult);
		if(save!=null){
			return true;
		}return false;
	}

	@Override
	public Consult selConsult(int id) {
		Consult consult= (Consult)this.sessionFactory.getCurrentSession().load(Consult.class, id);
		return consult;
	}

	@Override
	public void delConsult(Consult selConsult) {
		this.sessionFactory.getCurrentSession().delete(selConsult);
		
	}

	@Override
	public List<Consult> selTypeConsult(String type,int pageNum, int pageSize) {
		try {
			Query query = this.sessionFactory.getCurrentSession().createQuery("from Consult where type = ?");
			query.setParameter(0, type);
			query.setFirstResult((pageNum-1)*pageSize);
			query.setMaxResults(pageSize);
			List<Consult> list = query.list();
			return list;
		} catch (Exception e) {
			// TODO: handle exception
		}return null;
	}

	@Override
	public List<Consult> selDocConsult(Doctor doctor) {
		try {
			Query query = this.sessionFactory.getCurrentSession().createQuery("from Consult where doctor = ?");
			query.setParameter(0, doctor);
			List<Consult> list = query.list();
			return list;
		} catch (Exception e) {
			// TODO: handle exception
		}return null;
	}

	@Override
	public void updConsult(int id, String content, String type) {
		Query query = this.sessionFactory.getCurrentSession().createQuery("update Consult set content=?,type=? where id=?");
		query.setParameter(0, content);
		query.setParameter(1, type);
		query.setParameter(2, id);
		query.executeUpdate();
	}

	@Override
	public int selCountConsult() {
		try {
			Query query = this.sessionFactory.getCurrentSession().createQuery("select count(*) from Consult");
			int count=((Long) query.setCacheable(true).uniqueResult()).intValue();
			return count;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return 0;
	}

	@Override
	public boolean updUser(String name, String password, String sex, String eamil) {
		Query query = this.sessionFactory.getCurrentSession().createQuery("update User set password=?,sex=?,email=? where name=?");
		query.setParameter(0, password);
		query.setParameter(1, sex);
		query.setParameter(2, eamil);
		query.setParameter(3, name);
		int executeUpdate = query.executeUpdate();
		if(executeUpdate==1){
			return true;
		}
		return false;
	}
	/**
	 * 添加咨询且扣除用户钱 到咨询表
	 * @param consult
	 * @param doctor
	 * @param user
	 * @return 
	 */
	public boolean addConsultByUser(Consult consult,Doctor doctor,User user){
	
		String hql="select money from User where email=?";
		double money=(double) this.sessionFactory.getCurrentSession().createQuery(hql).setParameter(0, user.getEmail()).uniqueResult();		
		if (money<consult.getMoney() && money>0) {
			return false;
		}else{
			String hql1="update User set money=money-? where email=?";
			this.sessionFactory.getCurrentSession().createQuery(hql1).setParameter(0, consult.getMoney()).setParameter(1, user.getEmail()).executeUpdate();						
			consult.setContent(consult.getContent());
			consult.setPatient(false);	
			consult.setUser(user);
			consult.setMoney(consult.getMoney());
			consult.setType(null);
			consult.setDoctor(doctor);
			this.sessionFactory.getCurrentSession().save(consult);
			return true;
		}
				
	}
	/**
	 * 通过name查询全部信息
	 * @param name
	 * @return
	 */
	 public Doctor findDoctorByName(String name){
		String hql="from Doctor where name=?";
		Doctor doctor= (Doctor) this.sessionFactory.getCurrentSession().createQuery(hql).setParameter(0, name).uniqueResult();
		return doctor;
		 
	 }
	 /**
	  * 用户删除咨询
	  * @param consult
	  * @param user
	  */
	 public void deleteConsultByUser(Consult consult,User user){
		 consult=this.sessionFactory.getCurrentSession().get(Consult.class, consult.getId());
		 String hql="update User set money=money+? where email=?";
		 this.sessionFactory.getCurrentSession().createQuery(hql).setParameter(0, consult.getMoney()).setParameter(1, user.getEmail()).executeUpdate();
		 this.sessionFactory.getCurrentSession().delete(consult);
		 
	 }
}
